<?php

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Model;

class SalesStrategy extends Model
{

    protected $table = 'sales_strategy';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U'; // 时间戳

    /**
     * 获取列表
     * @param array $param
     * @param int $limit
     * @return mixed
     */
    public static function getList($param = [], $limit = 15)
    {
        $where = [];
        if (isset($param['type']) && !empty($param['type'])) {
            $where[] = ['type', '=', $param['type']];
        }
        if (isset($param['start']) && !empty($param['start'])) {
            $where[] = ['start', '>', strtotime($param['start'])];
        }
        if (isset($param['end']) && !empty($param['end'])) {
            $where[] = ['end', '<', strtotime($param['end'])];
        }

        $result = self::where($where)
            ->orderBy('id', 'desc')
            ->paginate($limit);
        return $result;
    }

    /**
     * 获取策略详情
     * @param array $where
     * @return mixed
     */
    public static function getInfo($param = [])
    {
        $where = [];
        if (isset($param['id']) && $param['id'] !== null) {
            $where['id'] = (int)$param['id'];
        }
        if (!count($where)) {
            return [];
        }

        $result = self::where($where)->orderBy('id', 'asc')->first();
        return $result;
    }

    /**
     * 添加策略
     * @param array $where
     * @return mixed
     */
    public static function add($info)
    {
        $time = time();
        $info['updated_at'] = $time;
        $info['created_at'] = $time;

        return self::insertGetId($info);
    }

    /**
     * 编辑策略
     * @param $id
     * @param $info
     * @return mixed
     */
    public static function edit($id, $info)
    {
        $info['updated_at'] = time();

        return self::where(['id' => $id])->update($info);
    }

    /**
     * 删除策略
     * @param $id
     * @return mixed
     */
    public static function del($id)
    {
        return self::where(['id' => $id])->delete();
    }
}